package com.atzly.community.event;

import com.alibaba.fastjson.JSONObject;
import com.atzly.community.entity.Event;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

/**
 * 事件生产者
 *
 * @author zlysimida
 * @date 2021/9/1 - 9:47
 */
@Component
@Slf4j
public class EventProducter {

    @Autowired
    private KafkaTemplate kafkaTemplate;

    public void sendMessage(Event event) {
        if (event == null || event.getTopic() == null) {
            log.error("发送的事件对象不能为空!");
            return;
        }
        kafkaTemplate.send(event.getTopic(), JSONObject.toJSONString(event));
    }
}
